package org.jeecg.modules.networking.util;

import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.util.encoders.Hex;

public class SM3Util {

    /**
     * 对输入的字符串进行SM3加密
     * @param input 需要加密的字符串
     * @return 加密后的16进制字符串
     */
    public static String encryptSM3(String input) {
        SM3Digest digest = new SM3Digest();
        byte[] bytes = input.getBytes();
        digest.update(bytes, 0, bytes.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash, 0);
        return Hex.toHexString(hash);
    }

    public static void main(String[] args) {
        // 示例用户名、请求时间和分配的密码
        String username = "yourUsername";
        long requestTimestamp = System.currentTimeMillis() / 1000; // 获取当前时间戳
        String allocPassword = "yourAllocatedPassword";

        // 构建待加密字符串
        String toEncrypt = username + requestTimestamp + allocPassword;

        // 执行SM3加密
        String encryptedPassword = encryptSM3(toEncrypt);
        System.out.println("Encrypted Password: " + encryptedPassword);
    }
}